package com.iring.service.common.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iring.dao.common.PayOrderDao;
import com.iring.dao.common.view.OrderViewDao;
import com.iring.domain.common.view.OrderView;
import com.iring.domain.pay.check.AlipayCheck;
import com.iring.domain.pay.check.PaypalCheck;
import com.iring.domain.pay.info.AliPayInfo;
import com.iring.domain.pay.info.PayPalInfo;
import com.iring.domain.pay.res.AliPayRes;
import com.iring.domain.pay.res.PaypalRes;
import com.iring.service.common.OrderService;
import com.iring.service.common.OrderViewService;
import com.iring.service.pay.PayService;

/**
 * 订单查询服务
 *
 * @author wanggan
 */

@Service
public class OrderViewServiceImpl implements OrderViewService {

    private static final Logger logger = LoggerFactory.getLogger(OrderViewServiceImpl.class);

    @Autowired
    OrderViewDao orderViewDao;

    @Autowired
    PayOrderDao payOrderDao;

    @Autowired
    OrderService orderService;

    @Resource(name = "paypal")
    PayService<PayPalInfo, PaypalRes,PaypalCheck> paypalService;

    @Resource(name = "alipay")
    PayService<AliPayInfo,AliPayRes,AlipayCheck> alipayService;


    @Override
    public List<OrderView> listAllOrder(String username) {
        //查询当前未完成订单
//        List<String> list = new ArrayList<>();
//        list.add(PayConfig.ORDER_CREATED);
//        list.add(PayConfig.WAIT_BUYER_PAY);
//        List<PayOrder> orderList = payOrderDao.findByUsernameAndPayStatusIn(username,list);
//        for(PayOrder payOrder:orderList){
//            //如果是paypal的话，请求paypal
//            String orderType = payOrder.getOrderType();
//            String tradeNo = payOrder.getTradeNo();
//
//            if(PayConfig.PAYPAL.equals(orderType)){
//
//                PaypalCheck paypalCheck = new PaypalCheck();
//                paypalCheck.setTradeNo(tradeNo);
//                paypalCheck = paypalService.checkPay(paypalCheck);
//
//                if (paypalCheck.getPayResult()) {
//                    String paystatus = paypalCheck.getPayStatus();
//                    String payerid = paypalCheck.getPayerId();
//                    orderService.updateOrderStatus(tradeNo,paystatus,payerid);
//                }
//            }else if(PayConfig.ALIPAY.equals(orderType)){
//                if(null == tradeNo){
//                    continue;
//                }
//                AlipayCheck basePay = new AlipayCheck();
//                basePay.setTradeNo(tradeNo);
//                basePay = alipayService.checkPay(basePay);
//                if(basePay.getPayResult()){
//                    String paystatus = basePay.getPayStatus();
//                    String payerid = basePay.getPayerId();
//                    orderService.updateOrderStatus(tradeNo,paystatus,payerid);
//                }
//            }
//        }
        return orderViewDao.findByUsernameOrderByOrderCreateTimeDesc(username);
    }
}
